package ua.com.jpy.entity.customer.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.code.morphia.Morphia;
import com.google.code.morphia.query.Query;
import com.mongodb.Mongo;

import ua.com.jpy.common.basic.dao.JpyBasicDao;
import ua.com.jpy.entity.customer.Customer;

/**
 * @author LSD25
 *
 */
public class CustomerDao extends JpyBasicDao<Customer, ObjectId> implements ICustomerDao {
	
	private static final Log log = LogFactory.getLog(CustomerDao.class);
	
	private Customer customer = null;
	
	@Autowired
	public CustomerDao(Mongo mongo, Morphia morphia, String dbName, String username, String password, boolean isAuthenticated) {
		super(mongo, morphia, dbName, username, password, isAuthenticated);
	}
	
	public Customer find(String login) {
		Query<Customer> query = getDatastore().createQuery(Customer.class);
		query = query.field("login").equal(login);
		customer = query.get();
		if(customer != null) {
			log.info("Customer was found, login: " + login);
			return customer;
		}
		log.info("Customer was not found with login: " + login);
		return null;
	}
	
}
